import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
export default defineConfig(({ mode, command }) => {
	const env = loadEnv(mode, process.cwd());
	// let reg = new RegExp(`^\\${env.VITE_BASE_API}`);
	// console.log(env, reg, "env-vite.config.ts");
	return {
		outDir: "dist",
		plugins: [
			vue(),
			// ViteComponents(() => {
			//   directoryAsNamespace: true;
			// }),
			AutoImport({
				resolvers: [ElementPlusResolver()]
			}),
			Components({
				resolvers: [ElementPlusResolver()]
			})
		],
		css: {
			preprocessorOptions: {
				scss: {
					additionalData: `@use "~/styles/element/index.scss" as *;`
				},
				less: {
					additionalData: `@import "@/styles/var/index.less";`
				}
			}
		},
		resolve: {
			alias: {
				"@": fileURLToPath(new URL("./src", import.meta.url))
			}
		},
		extensions: [".js", ".ts", ".tsx", ".jsx"],
		server: {
			host: "0.0.0.0",
			port: 8090,
			https: false,
			hmr: {
				overlay: false
			},
			// 设置反向代理，跨域
			proxy: {
				[env.VITE_BASE_API]: {
					// 后台地址
					target: env.VITE_BASE_URL,
					changeOrigin: true,
					// pathRewrite: {
					//   [`^${env.VITE_BASE_API}`]: ""
					// }
					rewrite: (path) => path.replace(/^\/api/, "")
				}
			}
		}

		// build: {
		//   rollupOptions: {
		//     output: {
		//       manualChunks: {
		//         'group-first': [
		//           './src/views/page/aside',
		//           './src/views/page/asideItem',
		//           './src/views/page/header'
		//         ],
		//         'group-second': [
		//           './src/views/page/layout',
		//           './src/views/page/tabList'
		//         ],
		//       },
		//     },
		//   },
		// },
	};
});
